﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Tasker.Common.Web
{
    /// <summary>
    /// Store property information in HttpContext
    /// </summary>
    /// <typeparam name="T">Property type</typeparam>
    public sealed class HttpContextProperty<T>
    {
        #region Fields

        private readonly string _name = "ContextProperty-" + Guid.NewGuid().ToString();

        #endregion

        /// <summary>
        /// Gets a value indicating whether the current property has a value.
        /// </summary>
        public bool HasValue
        {
            get
            {
                return HttpContext.Current.Items.Contains(_name) && HttpContext.Current.Items[_name] is T;
            }
        }

        /// <summary>
        /// Gets or sets property value
        /// </summary>
        public T Value
        {
            get
            {
                if (HasValue)
                    return (T)HttpContext.Current.Items[_name];
                else
                    return default(T);
            }
            set
            {
                HttpContext.Current.Items[_name] = value;
            }
        }
    }
}
